var SERVER_PORT = "http://120.77.223.88:8080/red/";
var shoppingCart = new Vue({
    el: '#app-shoppingCart',
    data: {
        //登录账户
        isLogin:false,
        phone: "",
        nickname:"",

        //购物车
        showGoods:false,
        goods:[],

        totalPrice:0,

        isSelectAll:false,
        selectMsg:"全选",
        selected:[]
    },
    methods: {
        //退出登录
        quitLogin: function () {
            this.isLogin = false;
            this.phone = "";
            this.nickname = "";
            window.localStorage.setItem("isLogin", false);
            window.location.reload(true);
        },

        //获取购物车物品
        getCartItem: function () {
            console.log("phone:"+this.phone);
            this.$http.post(SERVER_PORT + "requestGoodsInCart", {
                phone: this.phone
            }, {emulateJSON: true})
                .then((res) => {
                    if(res.data.status === 1){
                        console.log(res.data.data);
                        shoppingCart.goods = res.data.data;
                        if(shoppingCart.goods.length !== 0)
                            shoppingCart.showGoods = true;
                    } else {
                        alert("获取数据失败");
                    }
                }, (err) => {
                    alert("获取数据失败");
                    console.log(err.data);
                });
        },

        //移除购物车
        removeGoods: function (index) {
            this.$http.post(SERVER_PORT + "deleteGoodsInCart", {
                phone: shoppingCart.phone,
                goods_ID: shoppingCart.goods[index].goods_ID
            }, {emulateJSON:true})
                .then((res) => {
                    if(res.data.status == 1){
                        shoppingCart.goods.splice(index, 1);
                    }else {
                        alert("收藏失败");
                    }
                }, (err) => {
                    alert("删除失败");
                    console.log(err.data);
                });
        },

        //增加数量
        addItem: function (index) {
            this.$http.post(SERVER_PORT + "deleteGoodsInCart", {
                phone: shoppingCart.phone,
                goods_ID: shoppingCart.goods[index].goods_ID,
                quantity: shoppingCart.goods[index].quantity+1
            }, {emulateJSON:true})
                .then((res) => {
                    if(res.data.status == 1){
                        shoppingCart.goods[index].quantity++;
                        this.computeAmount();
                    }else {
                        alert("收藏失败");
                    }
                }, (err) => {
                    alert("增加失败");
                    console.log(err.data);
                });
        },

        //减少
        reduceItem: function (index) {
            var quantity = this.goods[index].quantity;
            if(quantity > 1){
                this.$http.post(SERVER_PORT + "deleteGoodsInCart", {
                    phone: shoppingCart.phone,
                    goods_ID: shoppingCart.goods[index].goods_ID,
                    quantity: quantity-1
                }, {emulateJSON:true})
                    .then((res) => {
                        if(res.data.status == 1){
                            shoppingCart.goods[index].quantity--;
                            this.computeAmount();
                        }else {
                            alert("收藏失败");
                        }
                    }, (err) => {
                        alert("减少失败");
                        console.log(err.data);
                    });
            }
        },

        //收藏商品
        collectGoods: function (index) {
            this.$http.post(SERVER_PORT + "collectGoodsInStore", {
                phone: shoppingCart.phone,
                goods_ID: shoppingCart.goods[index].goods_ID
            }, {emulateJSON:true})
                .then((res) => {
                    if(res.data.status == 1){
                        console.log("收藏成功");
                    }else {
                        alert("收藏失败");
                    }
                }, (err) => {
                    alert("收藏失败");
                    console.log(err.data);
                });
        },

        //选中 取消选中
        selectItem: function (index) {
            var i = this.selected.indexOf(index);
            if(i === -1) {//未选中
                this.selected.push(index);
                if(this.selected.length === this.goods.length){
                    this.selectMsg = "取消全选";
                    this.isSelectAll = true;
                }
            } else {
                this.selected.splice(i, 1);
                if(this.selectMsg === "取消全选"){
                    this.selectMsg = "全选";
                    this.isSelectAll = false;
                }
            }
            this.computeAmount();
        },
        //全选 取消全选
        selectAll: function () {
            if(this.selected.length !== this.goods.length){//全选
                this.selected.splice(0, this.selected.length);
                for(var i=0;i<this.goods.length;i++){
                    this.selected.push(i);
                }
                this.selectMsg = "取消全选";
                this.isSelectAll = true;
            } else {
                this.selected.splice(0, this.selected.length);
                this.selectMsg = "全选";
                this.isSelectAll = false;
            }
            this.computeAmount();
        },
        //检查是否选中
        checkIfSelect: function (index) {
            return this.selected.indexOf(index) !== -1;
        },

        //删除选中
        deleteSelected: function () {
            for(var i=0;i<this.selected.length;i++){
                this.removeGoods(this.selected[i]);
            }
            this.selected.splice(0, this.selected.length);
            this.selectMsg = "全选";
            this.isSelectAll = false;
            this.computeAmount();
        },

        //收藏选中
        collectSelected: function () {
            for(var i=0;i<this.selected.length;i++){
                this.collectGoods(this.selected[i]);
            }
        },
        
        //计算总额
        computeAmount: function () {
            var amount = 0;
            for(var i=0;i<this.selected.length;i++){
                amount += this.goods[this.selected[i]].price * this.goods[this.selected[i]].quantity;
            }
            this.totalPrice = amount;
        },

        onTapToGoodDetail:function(ID){
            window.localStorage.setItem("good_id",ID);
            window.location.href="pro-detail.html";
        }
    },

    created: function () {
        console.log("检查登录");
        if(window.localStorage.getItem("isLogin") === undefined || window.localStorage.getItem("isLogin") === "false"){
            alert("请先登录");
            window.location.href="login.html";
        }else{
            this.isLogin = true;
            this.nickname = window.localStorage.getItem("nickname");
            this.phone = window.localStorage.getItem('phone');
            this.getCartItem();
        }
    }
});